from django.db import models
from django.contrib.auth.models import User

class Project(models.Model):
	project_name = models.CharField(maxlength=50)
	description = models.TextField()
	public = models.BooleanField()
	def __unicode__(self):
		return self.project_name
	class Admin:
		pass

class ProjectUser(models.Model):
	project = models.ForeignKey(Project)#, edit_inline=models.TABULAR)
	user = models.ForeignKey(User)#, core=True)
	can_view = models.BooleanField()#core=True)
	can_update = models.BooleanField()#core=True)
	can_admin = models.BooleanField()#core=True)
	def __unicode__(self):
		return "%s (%s)" % (self.project, self.user)
	class Meta:
		unique_together = (('project', 'user'),)
	class Admin:
		pass

class Question(models.Model):
	project = models.ForeignKey(Project, null=True)
	question = models.TextField()
	num_replies = models.IntegerField(default=0)
	answered = models.CharField(maxlength=1, default='N') # choices = 'Y', 'N' or 'P' (for partial)
	submitter = models.ForeignKey(User, null=True)
	entry_date = models.DateTimeField('Date Asked', auto_now_add=True)
	modification_date = models.DateTimeField('Updated', auto_now_add=True, auto_now=True)
	def __unicode__(self):
		return self.question[:80]
	def status(self):
		if self.answered == 'Y':
			return 'CLOSED'
		else:
			return 'OPEN'
	class Admin:
		fields = (
			(None, {'fields': ('question', 'project',)}),
			(None, {'fields': ('submitter', 'num_replies', 'answered',)}),
			('Date Information', {
				'fields': ('entry_date', 'modification_date',),
				'classes': 'collapse'
			}),
		)
		list_display = ('question', 'project', 'submitter', 'entry_date', 'num_replies', 'answered')
		list_filter = ['entry_date', 'project',]
		search_fields = ['question']
		date_hierarchy = 'entry_date'

class QuestionTag(models.Model):
	question = models.ForeignKey(Question, edit_inline=models.STACKED)
	tag = models.CharField(maxlength=20, core=True)
	def __unicode__(self):
		return self.tag
	class Meta:
		unique_together = (('question', 'tag'),)
	class Admin:
		pass

class Reply(models.Model):
	reply = models.TextField()
	question = models.ForeignKey(Question)
	submitter = models.ForeignKey(User, null=True)
	entry_date = models.DateTimeField('Date Entered', auto_now_add=True)
	def __unicode__(self):
		return self.reply[:80]
	class Admin:
		pass

